package zozopo.warehouse;


import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;



import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.warehouse.operations.GoodsOS;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


public class UploadImageForGoodsAction extends ActionSupport implements Action ,SessionAware,ServletRequestAware,ServletResponseAware{
	
	private static final String limitedCode ="WAREHOUSE.GOODS.MODIFY";
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Log log = LogFactory.getLog(LogFactory.class);
	
	private Map<Object, Object> session;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	private static final int BUFFER_SIZE = 16 * 1024;

	private static final String TEXT_TITLE = "Zozopo.com";

	private static final String WATER_IMG_NAME = "z.png";
	
	private static final String UPLOAD_IMAGES_PATH = "goods";
	
	private static final String UPLOAD_FIRST_IMAGES_PATH = "warehouse";

	// 输入参数:上传过来的文件路径
	private File file;
	private String imageurl;
	private String floatposition;
	private String resizesize;
	private String releaseKey;
	
	private int goodsid;
	
	// 输出参数
	private String uploadFileName;
	private String imageFileName;
	private String imageUrl;
	
	private String operationResult;
	
	public String getOperationResult() {
		return operationResult;
	}

	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}

	public String getReleaseKey() {
		return releaseKey;
	}

	public void setReleaseKey(String releaseKey) {
		this.releaseKey = releaseKey;
	}

	
	

	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}

	
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		response = arg0;
		//response.setCharacterEncoding("GB2312");
		//response.setContentType("text/xml; charset=gb2312");
	}

	

	public String getImageFileName() {
		return imageFileName;
	}

	// 输出参数:原图保存路径
	private String ipath;

	// 输出参数:缩略图保存路径
	private String imgPath;

	// 输出参数
	private String json;
	//输出参数
	private String url;

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	public String execute(){
		
		operationResult ="上传图片失败";
	

		try {
			uploadImage();
			response.getWriter().write(imageUrl);
			
			response.getWriter().flush();
			response.getWriter().close();
			
		} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		
		
		operationResult ="上传图片成功";
		return NONE;
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未有添加产品权限.");
			return;
		}
	}
	
	private String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}


	private void copy(File src, File dist) throws Exception {
		log.debug("[src]--" + src);
		log.debug("[dist]--" + dist);

		try {
		InputStream in = null;
		OutputStream out = null;
		try {
		in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
		out = new BufferedOutputStream(new FileOutputStream(dist), BUFFER_SIZE);

		byte[] buf = new byte[BUFFER_SIZE];
		while (in.read(buf) > 0) {
		out.write(buf);
		}
		out.close();
		in.close();
		} catch (FileNotFoundException e) {
		e.printStackTrace();
		} catch (IOException e) {
		e.printStackTrace();
		} finally {
		if (in != null)
		in.close();
		if (out != null)
		out.close();
		}
		} catch (Exception e) {
		e.printStackTrace();
		throw new Exception(e);
		}

	}


	public String uploadImage() throws Exception {
		log.debug("[file]--" + file);
		log.debug("[file name]--" + imageFileName);
		uploadFileName = imageFileName;
		
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
		
		imageFileName = new Date().getTime()  + "_" + goodsid + getExtention(imageFileName); 

		// 得到文件存放路径
		log.debug("[imageFileName]--" + imageFileName);
		String dir = ServletActionContext.getServletContext().getRealPath("/"+UPLOAD_FIRST_IMAGES_PATH+"/"+UPLOAD_IMAGES_PATH+"/"+goodsid);
		
		File dirs = new File(dir);
		
		//清空文件
		delFolder(dir);
		
		if (!dirs.exists()){
			dirs.mkdir();
		}
		

		// 使用原来的文件名保存图片
		String path = dir + "/" + imageFileName;
		File imageFile = new File(path);

		copy(file, imageFile);

		// 缩放
		zoom(imageFile,goodsid+"");
	
		//删除大图
		imageFile.delete();
		
		// 给大图添加文字水印
		//watermark(imageFile);
		// 给大图添加图片水印，可以是gif或png格式
		//imageWaterMark(imageFile);
		
		// 创建子目录 得到添加水印后的图片的存储路径,子目录只能一级一级的建
		String dist = dir + "/water";
		File outFile = new File(dist);
		if (!outFile.exists())
		outFile.mkdir();
		File sImgpath = new File(dist + "/" + imageFileName);

		// 给小图添加文字水印
		// watermark(sImgpath);
		// 给小图添加图片水印，可以是gif或png格式
		//imageWaterMark(sImgpath);

		// 大图路径
		ipath = UPLOAD_IMAGES_PATH+"/"+goodsid+"/" + imageFileName;
		// 小图路径
		imgPath = UPLOAD_IMAGES_PATH+"/"+goodsid+"/water/" + imageFileName;
		
		imageUrl = "/"+UPLOAD_FIRST_IMAGES_PATH+"/"+imgPath;
		
		GoodsOS goodsOS = new GoodsOS();
		goodsOS.upadteGoodsImageByGoodsid(goodsid,imageUrl);
		goodsOS.releaseConnection();
		
		return imageUrl;
	}


	public void zoom(File imageFile,String userid) throws Exception {
		String imgType = "jpg";
		log.debug("[zoom][imageFile]--" + imageFile);
		try {

		// 缩略图存放路径
		File todir = new File(ServletActionContext.getServletContext().getRealPath("/"+UPLOAD_FIRST_IMAGES_PATH+"/"+UPLOAD_IMAGES_PATH) +"/"+userid+ "/water");
		if (!todir.exists()) {
		todir.mkdir();
		}

		if (!imageFile.isFile())
		throw new Exception(imageFile + " is not image file error in CreateThumbnail!");

		File sImg = new File(todir, imageFileName);
		
		
		if (sImg.getName().toLowerCase().endsWith(".png")) {
			imgType = "PNG";
		}
		
		BufferedImage Bi = ImageIO.read(imageFile);
		// 假设图片宽 高 最大为130 80,使用默认缩略算法
		Image Itemp = Bi.getScaledInstance(60, 60, Bi.SCALE_DEFAULT);

		double Ratio = 0.0;
		if ((Bi.getHeight() > 60) || (Bi.getWidth() > 60)) {
		if (Bi.getHeight() > Bi.getWidth())
		Ratio = 60.0 / Bi.getHeight();
		else
		Ratio = 60.0 / Bi.getWidth();
		
		AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
			Itemp = op.filter(Bi, null);//*/
		}

		ImageIO.write((BufferedImage) Itemp, imgType, sImg);

		} catch (IOException e) {
			e.printStackTrace();
			throw new Exception(e);
		}
	}


	public void watermark(File img) throws Exception {
		log.debug("[watermark file name]--" + img.getPath());
		try {

		if (!img.exists()) {
		throw new IllegalArgumentException("file not found!");
		}

		log.debug("[watermark][img]--" + img);

		// 创建一个FileInputStream对象从源图片获取数据流
		FileInputStream sFile = new FileInputStream(img);

		// 创建一个Image对象并以源图片数据流填充
		Image src = ImageIO.read(sFile);

		// 得到源图宽
		int width = src.getWidth(null);
		// 得到源图长
		int height = src.getHeight(null);

		// 创建一个BufferedImage来作为图像操作容器
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		// 创建一个绘图环境来进行绘制图象
		Graphics2D g = image.createGraphics();
		// 将原图像数据流载入这个BufferedImage
		log.debug("width:" + width + " height:" + height);
		g.drawImage(src, 0, 0, width, height, null);
		// 设定文本字体
		//g.setFont(new Font("宋体", Font.BOLD, 28));
		String rand = TEXT_TITLE;
		// 设定文本颜色
		g.setColor(Color.blue);
		// 设置透明度
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
		// 向BufferedImage写入文本字符,水印在图片上的坐标
		g.drawString(rand, width - (width - 20), height - (height - 60));
		// 使更改生效
		g.dispose();
		// 创建输出文件流
		FileOutputStream outi = new FileOutputStream(img);
		// 创建JPEG编码对象
		JPEGImageEncoder encodera = JPEGCodec.createJPEGEncoder(outi);
		// 对这个BufferedImage (image)进行JPEG编码
		encodera.encode(image);
		// 关闭输出文件流
		outi.close();
		sFile.close();

		} catch (IOException e) {
		e.printStackTrace();
		throw new Exception(e);
		}
	}


	public void imageWaterMark(File imgFile) throws Exception {
		try {
		// 目标文件
		Image src = ImageIO.read(imgFile);
		int wideth = src.getWidth(null);
		int height = src.getHeight(null);
		
		BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_BGR);
		
		Graphics2D g = image.createGraphics();
		g.drawImage(src, 0, 0, wideth, height, null);

		// 水印文件 路径
		String waterImgPath = ServletActionContext.getServletContext().getRealPath("/"+UPLOAD_FIRST_IMAGES_PATH+"/"+UPLOAD_IMAGES_PATH)+"/"+WATER_IMG_NAME;
		File waterFile = new File(waterImgPath);
		Image waterImg = ImageIO.read(waterFile);

		int w_wideth = waterImg.getWidth(null);
		int w_height = waterImg.getHeight(null);
		//g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
		//g.setComposite(AlphaComposite.Xor);
		
		//g.drawImage(waterImg, (wideth - w_wideth) / 2, (height - w_height) / 2, w_wideth, w_height, null);
		g.drawImage(waterImg, 5 , height-w_height, w_wideth, w_height, null); 
		// 水印文件结束

		g.dispose();
		//*/
		//ImageIO.write(image, "jpg", src);
		FileOutputStream out = new FileOutputStream(imgFile);
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		//JPEGEncodeParam param = new JPEGEncodeParam();
		//param.set
		//encoder.setJPEGEncodeParam()
		encoder.encode(image);
		out.close();
		
		} catch (Exception e) {
		e.printStackTrace();
		}
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getIpath() {
		return ipath;
	}

	public void setFileFileName(String fileName) {
		this.imageFileName = fileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getJson() {
		return json;
	}



	public String getImgPath() {
		return imgPath;
	}
	public String getImageurl() {
		return imageurl;
	}

	public void setImageurl(String imageurl) {
		this.imageurl = imageurl;
	}

	public String getFloatposition() {
		return floatposition;
	}

	public void setFloatposition(String floatposition) {
		this.floatposition = floatposition;
	}

	public String getResizesize() {
		return resizesize;
	}

	public void setResizesize(String resizesize) {
		this.resizesize = resizesize;
	}

	
	
	
	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public int getGoodsid() {
		return goodsid;
	}

	public void setGoodsid(int goodsid) {
		this.goodsid = goodsid;
	}

	public File getFile() {
		return file;
	}
	
	//删除指定文件夹下所有文件
	//param path 文件夹完整绝对路径
	   public static boolean delAllFile(String path) {
	       boolean flag = false;
	       File file = new File(path);
	       if (!file.exists()) {
	         return flag;
	       }
	       if (!file.isDirectory()) {
	         return flag;
	       }
	       String[] tempList = file.list();
	       File temp = null;
	       for (int i = 0; i < tempList.length; i++) {
	          if (path.endsWith(File.separator)) {
	             temp = new File(path + tempList[i]);
	          } else {
	              temp = new File(path + File.separator + tempList[i]);
	          }
	          if (temp.isFile()) {
	             temp.delete();
	          }
	          if (temp.isDirectory()) {
	             delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
	             delFolder(path + "/" + tempList[i]);//再删除空文件夹
	             flag = true;
	          }
	       }
	       return flag;
	     }
	

	//删除文件夹
	//param folderPath 文件夹完整绝对路径
	
	   public static void delFolder(String folderPath) {
	   try {
	      delAllFile(folderPath); //删除完里面所有内容
	      String filePath = folderPath;
	      filePath = filePath.toString();
	      java.io.File myFilePath = new java.io.File(filePath);
	      myFilePath.delete(); //删除空文件夹
	   } catch (Exception e) {
	     e.printStackTrace(); 
	   }
}

	public String getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}



}
